<?php
declare (strict_types=1);

namespace app\admin\controller\admin;

use app\common\model\AdminAdmin;
use app\common\model\AdminAdmin as M;
use app\common\model\SchoolIndex;
use app\common\model\StudyStudentClass;
use think\facade\Request;
use think\facade\Db;
use app\common\service\AdminAdmin as S;

class Admin extends \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck', 'AdminPermission'];

    // 列表
    public function index()
    {
        return $this->getAuto($this->fetch(), M::getList());
    }

    // 添加
    public function add()
    {
        $school = SchoolIndex::field('id,school_name')->select();
        return $this->getAuto($this->fetch('', ['school' => $school]), S::goAdd(Request::post()));
    }

    // 编辑
    public function edit($id)
    {
        return $this->getAuto($this->fetch('', ['model' => M::find($id)]), S::goEdit(Request::post(), $id));
    }

    // 状态
    public function status($id)
    {
        return $this->getJson(S::goStatus(Request::post('status'), $id));
    }

    // 删除
    public function remove($id)
    {
        return $this->getJson(S::goRemove($id));
    }

    // 批量删除
    public function batchRemove()
    {
        return $this->getJson(S::goBatchRemove(Request::post('ids')));
    }

    // 用户分配角色
    public function role($id)
    {
        return $this->getAuto($this->fetch('', M::getRole($id)), S::goRole(Request::post('roles'), $id));
    }

    // 用户分配直接权限
    public function permission($id)
    {
        return $this->getAuto($this->fetch('', M::getPermission($id)), S::goPermission(Request::post('permissions'), $id));
    }

    // 回收站
    public function recycle()
    {
        return $this->getAuto($this->fetch(), S::goRecycle());
    }

    // 用户日志
    public function log()
    {
        return $this->getAuto($this->fetch(), M::getLog());
    }

    // 清空日志
    public function removeLog()
    {
        return $this->getJson(Db::name('admin_admin_log')->delete(true));
    }

    /**
     * 工作人员列表
     * @return mixed|object|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function worker()
    {
        $school_id = 1;
        return $this->getAuto($this->fetch(), M::getWorkerList($school_id));
    }

    // 添加
    public function addWorker()
    {
        return $this->getAuto($this->fetch(), S::goAdd(Request::post()));
    }

    public function principal()
    {
        return $this->getAuto($this->fetch(), M::getPrincipalList());
    }

    public function addPrincipal()
    {
        return $this->getAuto($this->fetch(), S::addPrincipal(Request::post()));
    }

    public function teacher()
    {
        return $this->getAuto($this->fetch(), M::getTeacherList());
    }

    public function addTeacher()
    {
        $role = dataSelect('bl_admin_admin_role', [['role_id', '=', 6]], '*', 'id');
        $admin_id = [];
        $admin = session('admin');
        if ($role)
            foreach ($role as $v) {
                $admin_id[] = $v['admin_id'];
            }
        $where = [];
        if ($admin['school_id']) {
            $where[] = ["school_id", "=", $admin['school_id']];
        }
       $list = AdminAdmin::order('id','desc')->field('id,nickname')->where($where)
            ->whereIn('id', $admin_id)
            ->select();

        $class = StudyStudentClass::order('id desc')->select();
        return $this->getAuto($this->fetch('', ['admin' => $list,'class'=>$class]), S::addTeacher(Request::post()));
    }
    // 编辑
    public function editTeacher($id)
    {
        $role = dataSelect('bl_admin_admin_role', [['role_id', '=', 6]], '*', 'id');
        $admin_id = [];
        $admin = session('admin');
        if ($role)
            foreach ($role as $v) {
                $admin_id[] = $v['admin_id'];
            }
        $where = [];
        if ($admin['school_id']) {
            $where[] = ["school_id", "=", $admin['school_id']];
        }
        $list = AdminAdmin::order('id','desc')->field('id,nickname')->where($where)
            ->whereIn('id', $admin_id)
            ->select();
        $class = StudyStudentClass::order('id desc')->select();
        $class_id = Db::table('bl_class_teacher')->where('teacher_id',$id)->column('class_id');
        return $this->getAuto($this->fetch('', ['admin'=>$list,'model' => M::find($id),'class'=>$class,'checked'=>$class_id]), S::goEditTeacher(Request::post(), $id));
    }

    // 编辑
    public function editAdmin($id)
    {
        return $this->getAuto($this->fetch('', ['model' => M::find($id)]), S::goEdit(Request::post(), $id));
    }

    /**
     * 招生人员列表
     * @return mixed|object|string
     */
    public function recruit()
    {
        return $this->getAuto($this->fetch(), M::getRecruitList());
    }

    /**
     * 添加招生人员
     * @return mixed|object|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addRecruit()
    {
        $role = dataSelect('bl_admin_admin_role', [['role_id', '=', 5]], '*', 'id');
        $admin_id = [];
        if ($role)
            foreach ($role as $v) {
                $admin_id[] = $v['admin_id'];
            }
        $where = [];
        $admin = session('admin');
        if ($admin['school_id']) {
            $where[] = ["school_id", "=", $admin['school_id']];
        }
        $list = AdminAdmin::order('id','desc')->field('id,nickname')->where($where)
            ->whereIn('id', $admin_id)
            ->select();
        return $this->getAuto($this->fetch('', ['admin' => $list]), S::addRecruit(Request::post()));
    }

    /**
     * 财务人员列表
     * @return mixed|object|string
     * @throws \think\db\exception\DbException
     */
    public function finance()
    {
        return $this->getAuto($this->fetch(), M::getFinanceList());
    }

    public function addFinance()
    {
        return $this->getAuto($this->fetch(''), S::addFinance(Request::post()));
    }

    public function purchase()
    {
        return $this->getAuto($this->fetch(), M::getPurchaseList());
    }

    public function addPurchase()
    {
        return $this->getAuto($this->fetch(''), S::addPurchase(Request::post()));
    }

}
